<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles-extras" prefix="tilesx" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="security"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tag"%>

<script type="text/javascript">
$(document).ready(function()
{
  $("#dialogNewPassword").dialog({
    modal:true, draggable:false,resizable:false,disabled:true,closeOnEscape:false,autoOpen:false,show:"blind",width:320,height:150,
	title:'<img src="pages/images/themed/portalHeaderIcon.gif" width="12" height="12"/> &nbsp; Reset Password Confirmation',
	open: function(event, ui) {$(this).parent().children().children('.ui-dialog-titlebar-close').hide();},
	buttons:{"Continue": function() {$(this).dialog('close');location.href='userList.htm';}}
  });

  if ($(parseIdForJQuery("adminCommand.showNewPassword")).val() == 'true')
  {
    $('#dialogNewPassword').css("display", "block").css("visibility", "visible").dialog('open');
  }
});

function openSaveChangesDialog()
{
  afterFieldEdit('<spring:message code="field.username"/>', 'string', 'user.username', 'false', new Array());
  afterFieldEdit('<spring:message code="field.role"/>', 'string', 'rolePk', 'false', new Array());
  var isNewField = document.getElementById('adminCommand.isNew');
  if (isNewField.value == 'true')
  {
	afterFieldEdit('<spring:message code="field.password"/>', 'string', 'user.password', 'false', new Array());
	afterFieldEdit('<spring:message code="field.confirm.password"/>', 'string', 'passwordConfirm', 'false', new Array());
  }
  setTimeout("checkErrorsForSaveChangesDialog()", 100);
}

function validateUsername(itemName)
{
  var userPkField = document.getElementById('adminCommand.user.pk');
  var newValueField = document.getElementById(itemName);
  $.ajax({url: 'validation.htm', type: 'POST',
  	      data : {newValueFld : newValueField.value,
	    	      userPkFld : userPkField.value,
	    	      adminRequestedAction:"validateUsername"}}).success( function (aRes)
	    	    		                                              {
	    	    	                                                    errorMessage = aRes;
																		if (errorMessage != "")
																		{
																		  changeClassToError(newValueField);
																		  addError(itemName, errorMessage);
																		}
																		else
																		{
																		  fieldEdit(itemName);
																		  removeError(itemName);
																		}
  });
}

function validatePassword(itemName)
{
	var newValueField = document.getElementById(itemName);

	$.ajax({url: 'validation.htm', type: 'POST',
  	      data : {newValue : newValueField.value,
	    	      userName : null,
	    	      adminRequestedAction:"validatePassword"}}).success( function (aRes) {    errorMessage = aRes;
																		    		  if (errorMessage != "")
																		    		  {
																		    		    changeClassToError(newValueField);
																		    		    addError(itemName, errorMessage);
																		    		  }
																		    		  else
																		    		  {
																		    		    fieldEdit(itemName);
																		    		    removeError(itemName);
																		    		  }

																		    		  // Clear the password confirm
																		    		  var passwordConfirmField = document.getElementById('passwordConfirm');
																		    		  passwordConfirmField.value = '';

																		    		  fieldEdit('passwordConfirm');
																		    		  removeError('passwordConfirm');
	    		  													});
}

function validatePasswordConfirm(itemName, passwordItemName)
{
  var newValueField = document.getElementById(itemName).value;
  var passwordValueField = document.getElementById(passwordItemName).value;

  $.ajax({url: 'validation.htm', type: 'POST',
	      data : {newValue : newValueField,
	    	      userName : passwordValueField,
	    	      adminRequestedAction:"validatePasswordConfirm"}}).success( function (aRes) {    errorMessage = aRes;
																		    		  if (errorMessage != "")
																		    		  {
																		    		    changeClassToError(newValueField);
																		    		    addError(itemName, errorMessage);
																		    		  }
																		    		  else
																		    		  {
																		    		    fieldEdit(itemName);
																		    		    removeError(itemName);
																		    		  }
	    		  													});
}

function activateDeactivate()
{
	var isActive = $("#adminCommand\\.user\\.active").val();
	var htmlToInsert;

	if (isActive == 'true')
	{
		$("#adminCommand\\.user\\.active").val(false);

		htmlToInsert = "<tr>"
                     + "<td style='border-style:none;border-width:0px;border-color:white;font-size:14px;background-color:#ffff97'>"
                     + "Disabling User</td>"
                     + "</tr>";
	}
	else
	{
		$("#adminCommand\\.user\\.active").val(true);

		htmlToInsert = "<tr>"
                     + "<td style='border-style:none;border-width:0px;border-color:white;font-size:14px;background-color:#ffff97'>"
                     + "Enabling User</td>"
                     + "</tr>";
	}

	$("#activateDeactivateTable").html(htmlToInsert);
	$("#activateDeactivateTable").css('backgroundColor', '#ffff97');
	$("#activateDeactivateTd").css('backgroundColor', '#ffff97');

  var changeCountField = document.getElementById('changeCount');
  if (changeCountField != null)
  {
    changeCountField.value = parseInt(changeCountField.value) + 1;
    checkButtons(changeCountField.value);
  }
}

function lockUnlock()
{
	var isLocked = $("#adminCommand\\.user\\.locked").val();
	var htmlToInsert;

	if (isLocked == 'true')
	{
		$("#adminCommand\\.user\\.locked").val(false);

		htmlToInsert = "<tr>"
                 + "<td style='border-style:none;border-width:0px;border-color:white;font-size:14px;background-color:#ffff97'>"
                 + "Unlocking User</td>"
                 + "</tr>";
	}
	else
	{
		$("#adminCommand\\.user\\.locked").val(true);

		htmlToInsert = "<tr>"
                 + "<td style='border-style:none;border-width:0px;border-color:white;font-size:14px;background-color:#ffff97'>"
                 + "Locking User</td>"
                 + "</tr>";
	}

	$("#lockUnlockTable").html(htmlToInsert);
	$("#lockUnlockTable").css('backgroundColor', '#ffff97');
	$("#lockUnlockTd").css('backgroundColor', '#ffff97');

  var changeCountField = document.getElementById('changeCount');
  if (changeCountField != null)
  {
    changeCountField.value = parseInt(changeCountField.value) + 1;
    checkButtons(changeCountField.value);
  }
}

function enableSave()
{
  if ($(parseIdForJQuery("user.username")).val() != '')
  {
    var saveButton = $("#saveButton");
    saveButton.click(function() {openSaveChangesDialog();});
    saveButton.mouseover(function () {buttonHover(this);});
    saveButton.mouseout(function() {buttonNormal(this);});
    saveButton.addClass("button_input").removeClass("button_inputDisabled");
  }
}

function updateChange()
{
  if (changeCountField != null)
  {
	changeCountField.value = parseInt(changeCountField.value) + 1;
	checkButtons(changeCountField.value);
  }
}
</script>

<div id="dialogNewPassword" style="display:none;visibility:hidden;">
  <p>
    <span class="ui-icon ui-icon-help" style="float:left; margin:0 7px 50px 0;"></span>
    The password for ${adminCommand.user.username} has been reset to ${adminCommand.newPassword}
  </p>
</div>

<form id="adminForm" name="adminForm" method="post">

  <form:hidden path="adminCommand.adminRequestedAction" id="adminCommand.adminRequestedAction"/>
  <form:hidden path="adminCommand.isNew" id="adminCommand.isNew"/>
  <form:hidden path="adminCommand.user.active" id="adminCommand.user.active"/>
  <form:hidden path="adminCommand.user.locked" id="adminCommand.user.locked"/>
  <form:hidden path="adminCommand.user.pk" id="adminCommand.user.pk"/>

  <form:hidden path="adminCommand.showNewPassword" id="adminCommand.showNewPassword"/>
  <form:hidden path="adminCommand.newPassword" id="adminCommand.newPassword"/>

  <input type="hidden" id="callStatus" name="callStatus" value=""/>

  <c:if test="${not adminCommand.showNewPassword}">
  <tag:errors name="adminCommand"/>
  <table><jsp:include page="/pages/jsp/tiles/admin/errors.jsp"/></table>

  <table align="center">
    <tr>
      <td>
        <table class="adminEdit" align="center">
          <tr>
            <th align="center" width="200px"><spring:message code="field.username"/></th>
            <security:authorize ifAllGranted="ROLE_EDITABLE">
            <spring:bind path="adminCommand.user.username">
            <td width="400px">
              <c:choose>
              <c:when test="${adminCommand.loggedOnUserName != adminCommand.user.username && adminCommand.user.username != 'utilisoft'}">
              <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
              <input type="text" id="${status.expression}" name="${status.expression}" value="${status.value}" tabindex="1"
                     class="largeTextField" maxlength="30"
                     onkeyup="fieldEdit('${status.expression}');"
                     onchange="afterFieldEdit('<spring:message code="field.username"/>', 'string', '${status.expression}', 'false', new Array());validateUsername('${status.expression}');"/>&nbsp;*
              <div id="${status.expression}_errorField" class="errorText" style="visibility:hidden;display:none;"></div>
              </c:when>
              <c:otherwise>
              <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
              <input type="hidden" id="${status.expression}" name="${status.expression}" value="${status.value}"/>
              ${adminCommand.user.username}
              </c:otherwise>
              </c:choose>
            </td>
            </spring:bind>
            </security:authorize>
            <security:authorize ifNotGranted="ROLE_EDITABLE">
            <td width="400px">${adminCommand.user.username}</td>
            </security:authorize>
          </tr>

          <tr>
            <th align="center"><spring:message code="field.password"/></th>
            <security:authorize ifAllGranted="ROLE_EDITABLE">
            <spring:bind path="adminCommand.user.password">
            <td>
              <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
              <c:choose>
              <c:when test="${adminCommand.isNew}">
              <input type="password"
                     id="${status.expression}"
                     name="${status.expression}"
                     value="${status.value}"
                     class="largeTextField"
                     maxlength="30"
                     onkeyup="fieldEdit('${status.expression}');"
                     onchange="afterFieldEdit('<spring:message code="field.password"/>', 'string', '${status.expression}', 'false', new Array());validatePassword('${status.expression}');"/>&nbsp;*
              </c:when>
              <c:otherwise>
              <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
              ********
              </c:otherwise>
              </c:choose>
              <div id="${status.expression}_errorField" class="errorText" style="visibility:hidden;display:none;"></div>
            </td>
            </spring:bind>
            </security:authorize>
            <security:authorize ifNotGranted="ROLE_EDITABLE">
            <td>********</td>
            </security:authorize>
          </tr>

          <c:if test="${adminCommand.isNew}">
          <tr>
            <th align="center"><spring:message code="field.confirm.password"/></th>
            <td>
              <input type="hidden" id="passwordConfirm_originalValue" name="passwordConfirm_originalValue" value=""/>
              <input type="password"
                     id="passwordConfirm"
                     name="passwordConfirm"
                     value=""
                     class="largeTextField"
                     maxlength="30"
                     onkeyup="fieldEdit('passwordConfirm');"
                     onchange="afterFieldEdit('<spring:message code="field.confirm.password"/>', 'string', 'passwordConfirm', 'false', new Array());validatePasswordConfirm('passwordConfirm', 'user.password');"/>&nbsp;*
              <div id="passwordConfirm_errorField" class="errorText" style="visibility:hidden;display:none;"></div>
            </td>
          </tr>
          </c:if>

          <tr>
            <th align="center"><spring:message code="field.role"/></th>
            <security:authorize ifAllGranted="ROLE_EDITABLE">
            <spring:bind path="adminCommand.rolePk">
            <td>
              <c:choose>
              <c:when test="${adminCommand.user.username != 'utilisoft'}">
              <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
              <select id="${status.expression}" name="${status.expression}" class="largeComboField"
                      onchange="fieldEdit('${status.expression}');afterFieldEdit('<spring:message code="field.role"/>', 'string', '${status.expression}', 'false', new Array());">
                <c:forEach items="${roles}" var="item">
                <option value="${item.value}" <c:if test="${item.value == status.value}">selected</c:if>> ${item.name}</option>
                </c:forEach>
              </select>&nbsp;*
              <div id="${status.expression}_errorField" class="errorText" style="visibility:hidden;display:none;"></div>
              </c:when>
              <c:otherwise>
              <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
              <input type="hidden" id="${status.expression}" name="${status.expression}" value="${status.value}"/>
              ${adminCommand.roleName}
              </c:otherwise>
              </c:choose>
            </td>
            </spring:bind>
            </security:authorize>
            <security:authorize ifNotGranted="ROLE_EDITABLE">
            <td>${adminCommand.roleName}</td>
            </security:authorize>
          </tr>

          <c:choose>
          <c:when test="${adminCommand.isNew}">
          <form:hidden path="adminCommand.user.active"/>
          </c:when>
          <c:otherwise>
          <tr>
            <th><spring:message code="field.active"/></th>
            <security:authorize ifAllGranted="ROLE_EDITABLE">
            <td id="activateDeactivateTd">
              <c:choose>
              <c:when test="${adminCommand.user.active}">
              <table align="center" id="activateDeactivateTable">
                <tr>
                  <td style="border-style:none;border-width:0px;border-color:white;font-size:14px">User is active</td>
                  <c:if test="${adminCommand.loggedOnUserName != adminCommand.user.username}">
                  <td style="border-style:none;border-width:0px;border-color:white;">
                  <a id="activateDeactivateButton"
                     class="button_input" style="width:90px;min-width:90px;max-width:90px"
                     onclick="activateDeactivate();" onmouseover="buttonHover(this);" onmouseout="buttonNormal(this);">
                    <span><spring:message code="button.deactivate"/></span>
                  </a>
                  </td>
                  </c:if>
                </tr>
              </table>
              </c:when>
              <c:otherwise>
              <table align="center" id="activateDeactivateTable">
                <tr>
                  <td style="border-style:none;border-width:0px;border-color:white;font-size:14px">User is not active</td>
                  <c:if test="${adminCommand.loggedOnUserName != adminCommand.user.username}">
                  <td style="border-style:none;border-width:0px;border-color:white;">
                  <a id="activateDeactivateButton" class="button_input" style="width:90px;min-width:90px;max-width:90px"
                     onclick="activateDeactivate();" onmouseover="buttonHover(this);" onmouseout="buttonNormal(this);">
                    <span><spring:message code="button.activate"/></span>
                  </a>
                  </td>
                  </c:if>
                </tr>
              </table>
              </c:otherwise>
              </c:choose>
            </td>
            </security:authorize>
            <security:authorize ifNotGranted="ROLE_EDITABLE">
            <c:choose>
            <c:when test="${adminCommand.user.active}"><td>User is active</td></c:when>
            <c:otherwise><td>User is not active</td></c:otherwise>
            </c:choose>
            </security:authorize>
          </tr>
          </c:otherwise>
          </c:choose>

          <c:choose>
          <c:when test="${adminCommand.isNew}">
          <form:hidden path="adminCommand.user.locked"/>
          </c:when>
          <c:otherwise>
          <tr>
            <th><spring:message code="field.unlocked"/></th>
            <security:authorize ifAllGranted="ROLE_EDITABLE">
            <td id="lockUnlockTd">
              <c:choose>
              <c:when test="${adminCommand.user.locked}">
              <table align="center" id="lockUnlockTable">
                <tr>
                  <td style="border-style:none;border-width:0px;border-color:white;font-size:14px">User is locked</td>
                  <c:if test="${adminCommand.loggedOnUserName != adminCommand.user.username}">
                  <td style="border-style:none;border-width:0px;border-color:white;">
                  <a id="lockUnlockButton"
                     class="button_input"
                     style="width:90px;min-width:90px;max-width:90px"
                     onclick="lockUnlock();"
                     onmouseover="buttonHover(this);"
                     onmouseout="buttonNormal(this);">
                    <span><spring:message code="button.unlock"/></span>
                  </a>
                  </td>
                  </c:if>
                </tr>
              </table>
              </c:when>
              <c:otherwise>
              <table align="center" id="lockUnlockTable">
                <tr>
                  <td style="border-style:none;border-width:0px;border-color:white;font-size:14px">User is unlocked</td>
                  <c:if test="${adminCommand.loggedOnUserName != adminCommand.user.username}">
                  <td style="border-style:none;border-width:0px;border-color:white;">
                  <a id="lockUnlockButton"
                     class="button_input"
                     style="width:90px;min-width:90px;max-width:90px"
                     onclick="lockUnlock();"
                     onmouseover="buttonHover(this);"
                     onmouseout="buttonNormal(this);">
                    <span><spring:message code="button.lock"/></span>
                  </a>
                  </td>
                  </c:if>
                </tr>
              </table>
              </c:otherwise>
              </c:choose>
            </td>
            </security:authorize>
            <security:authorize ifNotGranted="ROLE_EDITABLE">
            <c:choose>
            <c:when test="${adminCommand.user.locked}"><td>User is locked</td></c:when>
            <c:otherwise><td>User is unlocked</td></c:otherwise>
            </c:choose>
            </security:authorize>
          </tr>
          </c:otherwise>
          </c:choose>

          <tr>
            <th>Forwarding URL</th>

            <security:authorize ifAllGranted="ROLE_EDITABLE">
            <td>
              <spring:bind path="adminCommand.forwardingUrlPk">
              <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
              <select id="${status.expression}" name="${status.expression}" class="largeComboField"
                      onchange="fieldEdit('${status.expression}');afterFieldEdit('Forwarding URL', 'string', '${status.expression}', 'false', new Array());">
                <c:forEach items="${forwardingUrls}" var="item"><option value="${item.value}" <c:if test="${item.value == status.value}">selected</c:if>> ${item.name}</option></c:forEach>
              </select>&nbsp;*
              <div id="${status.expression}_errorField" class="errorText" style="visibility:hidden;display:none;"></div>
              </spring:bind>
            </td>
            </security:authorize>
            <security:authorize ifNotGranted="ROLE_EDITABLE">
              <td>
              <select class="largeComboField" disabled="disabled">
                <c:forEach items="${forwardingUrls}" var="item"><option value="${item.value}" <c:if test="${item.value == adminCommand.forwardingUrlPk}">selected</c:if>> ${item.name}</option></c:forEach>
              </select>
              </td>
            </security:authorize>
          </tr>
        </table>
      </td>
    </tr>




    <tr>
      <td>
        <table align="center" style="margin-top:10px;">
          <tr>
            <td style="text-align: center;"><jsp:include page="/pages/jsp/tiles/admin/userEditButtons.jsp"/></td>
          </tr>
        </table>
      </td>
    </tr>
    <jsp:include page="/pages/jsp/tiles/admin/hoverConfirmation.jsp"/>
  </table>
  </c:if>
</form>
